package com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.service.core;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yonyou.iuap.ipaas.connector.sdk.api.model.ResponseAttributes;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.core.response.Response;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.core.response.query.PageData;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.core.response.query.QueryResponse;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.core.response.save.ConnectorSaveResponse;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.core.response.save.SaveResponse;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.core.response.save.SaveResponseData;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.core.response.save.SaveResponseMessage;
import com.yonyou.yonlinker.ysb.runtime.extension.api.runtime.operation.Result;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public final class ResponseHandleService {

    public ResponseHandleService() {
    }

    public Result<String, ResponseAttributes> buildFail(Response response) {
        Result.Builder<String, ResponseAttributes> builder = Result.builder();
        ResponseAttributes ra = new ResponseAttributes();
        ra.setSuccess(false);
        ra.setCode("999");
        ra.setMessage("All failed");
        builder.attributes(ra);
        builder.output(JSONUtil.toJsonStr(response));
        return builder.build();
    }

    public Result<String, ResponseAttributes> buildQueryResult(QueryResponse queryResponse) {
        if (!ObjectUtil.equal(queryResponse.getCode(), "200")) {
            return this.buildFail(queryResponse);
        } else {
            Result.Builder<String, ResponseAttributes> builder = Result.builder();
            ResponseAttributes ra = new ResponseAttributes();
            ra.setSuccess(true);
            ra.setCode("200");
            ra.setMessage("All success");
            if (ObjectUtil.isNotEmpty(queryResponse.getData())) {
                PageData pageData = queryResponse.getPageData();
                if (ObjectUtil.isNotEmpty(pageData)) {
                    Long pageIndex = pageData.getPageIndex();
                    Long pageSize = pageData.getPageSize();
                    Long totalCount = pageData.getTotalCount();
                    ra.setPageIndex(pageIndex);
                    ra.setPageSize(pageSize);
                    ra.setTotalCount(totalCount);
                    if (pageIndex != null && pageSize != null && totalCount != null) {
                        ra.setHaveNextPage(totalCount > pageIndex * pageSize);
                    }
                } else {
                    ra.setHaveNextPage(false);
                }

                builder.output(JSONUtil.toJsonStr(queryResponse.getData()));
            } else {
                builder.output(JSONUtil.createArray().toString());
            }

            builder.attributes(ra);
            return builder.build();
        }
    }

    public Result<String, ResponseAttributes> buildSingleSaveResult(SaveResponse saveResponse, boolean needBusinessData) {
        if (!ObjectUtil.equal(saveResponse.getCode(), "200")) {
            return this.buildFail(saveResponse);
        } else {
            ConnectorSaveResponse connectorSaveResponse = new ConnectorSaveResponse();
            List<Map<String, Object>> infos = new ArrayList();
            List<SaveResponseMessage> messages = new ArrayList();
            infos.add(this.buildInfosMap(saveResponse, needBusinessData));
            connectorSaveResponse.setInfos(infos);
            connectorSaveResponse.setMessages(messages);
            return this.buildSaveSuccess(connectorSaveResponse);
        }
    }

    public Result<String, ResponseAttributes> buildLoopSaveSuccess(List<SaveResponse> saveResponses, boolean needBusinessData) {
        Long successCount = 0L;
        Long failCount = 0L;
        List<Map<String, Object>> infos = new ArrayList();
        List<SaveResponseMessage> messages = new ArrayList();
        Iterator var7 = saveResponses.iterator();

        while(var7.hasNext()) {
            SaveResponse saveResponse = (SaveResponse)var7.next();
            if (ObjectUtil.equal(saveResponse.getCode(), "200")) {
                successCount = successCount + 1L;
                infos.add(this.buildInfosMap(saveResponse, needBusinessData));
            } else {
                failCount = failCount + 1L;
                messages.add(new SaveResponseMessage(saveResponse.getSourceUnique(), saveResponse.getMessage()));
            }
        }

        ConnectorSaveResponse connectorSaveResponse = new ConnectorSaveResponse();
        connectorSaveResponse.setCount(successCount + failCount);
        connectorSaveResponse.setSuccessCount(successCount);
        connectorSaveResponse.setFailCount(failCount);
        connectorSaveResponse.setInfos(infos);
        connectorSaveResponse.setMessages(messages);
        return this.buildSaveSuccess(connectorSaveResponse);
    }

    public Result<String, ResponseAttributes> buildBatchSaveSuccess(JSONArray jsonArray, String typeName, String typeValue, String targetName, String sourceName, String errMesName, boolean needBusinessData) {
        if (ObjectUtil.isNotEmpty(jsonArray)) {
            Long successCount = 0L;
            Long failCount = 0L;
            List<Map<String, Object>> infos = new ArrayList();
            List<SaveResponseMessage> messages = new ArrayList();

            for(int i = 0; i < jsonArray.size(); ++i) {
                JSONObject json = jsonArray.getJSONObject(i);
                if (ObjectUtil.equal(json.get(typeName), typeValue)) {
                    successCount = successCount + 1L;
                    Map<String, Object> saveResponseInfo = new HashMap();
                    if (Boolean.TRUE.equals(needBusinessData)) {
                        saveResponseInfo.putAll(Convert.toMap(String.class, Object.class, json));
                    }

                    saveResponseInfo.put("data", new SaveResponseData(json.getStr(sourceName), json.getStr(targetName)));
                    infos.add(saveResponseInfo);
                } else {
                    failCount = failCount + 1L;
                    messages.add(new SaveResponseMessage(json.getStr(sourceName), json.getStr(errMesName)));
                }
            }

            ConnectorSaveResponse connectorSaveResponse = new ConnectorSaveResponse();
            connectorSaveResponse.setCount(successCount + failCount);
            connectorSaveResponse.setSuccessCount(successCount);
            connectorSaveResponse.setFailCount(failCount);
            connectorSaveResponse.setInfos(infos);
            connectorSaveResponse.setMessages(messages);
            return this.buildSaveSuccess(connectorSaveResponse);
        } else {
            return this.buildFail(new Response());
        }
    }

    public Result<String, ResponseAttributes> buildLoopDeleteSuccess(List<SaveResponse> saveResponses) {
        Long successCount = 0L;
        Long failCount = 0L;
        Long deleteCount = 0L;
        List<SaveResponseMessage> messages = new ArrayList();
        List<Map<String, Object>> infos = new ArrayList();
        List<SaveResponseData> deleteList = new ArrayList();
        Iterator var8 = saveResponses.iterator();

        while(var8.hasNext()) {
            SaveResponse saveResponse = (SaveResponse)var8.next();
            if (ObjectUtil.equal(saveResponse.getCode(), "1")) {
                deleteCount = deleteCount + 1L;
                deleteList.add(new SaveResponseData(saveResponse.getSourceUnique(), saveResponse.getTargetUnique()));
            } else if (ObjectUtil.equal(saveResponse.getCode(), "200")) {
                successCount = successCount + 1L;
                infos.add(this.buildInfosMap(saveResponse, true));
            } else {
                failCount = failCount + 1L;
                messages.add(new SaveResponseMessage(saveResponse.getSourceUnique(), saveResponse.getMessage()));
            }
        }

        ConnectorSaveResponse connectorSaveResponse = new ConnectorSaveResponse();
        connectorSaveResponse.setCount(successCount + deleteCount + failCount);
        connectorSaveResponse.setSuccessCount(successCount);
        connectorSaveResponse.setFailCount(failCount);
        connectorSaveResponse.setDeleteCount(deleteCount);
        connectorSaveResponse.setInfos(infos);
        connectorSaveResponse.setMessages(messages);
        connectorSaveResponse.setDeleteList(deleteList);
        return this.buildSaveSuccess(connectorSaveResponse);
    }

    public Result<String, ResponseAttributes> buildBatchDeleteSuccess(JSONArray jsonArray, String typeName, String typeValue, String targetName, String sourceName, String errMesName) {
        Long successCount = 0L;
        Long failCount = 0L;
        Long deleteCount = 0L;
        List<SaveResponseMessage> messages = new ArrayList();
        List<SaveResponseData> deleteList = new ArrayList();

        for(int i = 0; i < jsonArray.size(); ++i) {
            JSONObject json = jsonArray.getJSONObject(i);
            if (ObjectUtil.equal(json.get(typeName), typeValue)) {
                deleteCount = deleteCount + 1L;
                deleteList.add(new SaveResponseData(json.getStr(sourceName), json.getStr(targetName)));
            } else {
                failCount = failCount + 1L;
                messages.add(new SaveResponseMessage(json.getStr(sourceName), json.getStr(errMesName)));
            }
        }

        ConnectorSaveResponse connectorSaveResponse = new ConnectorSaveResponse();
        connectorSaveResponse.setCount(deleteCount + failCount);
        connectorSaveResponse.setSuccessCount(successCount);
        connectorSaveResponse.setFailCount(failCount);
        connectorSaveResponse.setDeleteCount(deleteCount);
        connectorSaveResponse.setMessages(messages);
        connectorSaveResponse.setDeleteList(deleteList);
        return this.buildSaveSuccess(connectorSaveResponse);
    }

    private Result<String, ResponseAttributes> buildSaveSuccess(ConnectorSaveResponse connectorSaveResponse) {
        Result.Builder<String, ResponseAttributes> builder = Result.builder();
        ResponseAttributes ra = new ResponseAttributes();
        ra.setSuccess(true);
        ra.setCode("200");
        ra.setMessage("All success");
        String output = connectorSaveResponse == null ? JSONUtil.createObj().toString() : JSONUtil.toJsonStr(connectorSaveResponse);
        builder.output(output);
        return builder.attributes(ra).build();
    }

    private Map<String, Object> buildInfosMap(SaveResponse saveResponse, boolean needBusinessData) {
        Map<String, Object> saveResponseInfo = new HashMap();
        SaveResponseData saveResponseData = new SaveResponseData(saveResponse.getSourceUnique(), saveResponse.getTargetUnique());
        saveResponseInfo.put("data", saveResponseData);
        if (needBusinessData && ObjectUtil.isNotEmpty(saveResponse.getData())) {
            saveResponseInfo.putAll(Convert.toMap(String.class, Object.class, saveResponse.getData()));
        }

        return saveResponseInfo;
    }
}

